package gui;

import gui.treenodes.TransactionNode;
import gui.treeutil.TransactionOutlineModel;

import java.awt.Color;
import java.util.Collection;
import java.util.Date;

import org.netbeans.swing.outline.Outline;

import com.toedter.calendar.JDateChooserCellEditor;

import core.Transaction;

public class TransactionsTable extends Outline
{
	private static final TransactionRowModel ROW_MODEL = new  TransactionRowModel();
	private TransactionTreeModel currentModel;
	private Color color;

	public TransactionsTable(Color color, String rootName){
		this.color = color;
		currentModel = new TransactionTreeModel(rootName);
		setModel(TransactionOutlineModel.createOutlineModel(currentModel, ROW_MODEL, false, "Typ"));
		setDefaultEditor(Date.class, new JDateChooserCellEditor());
		System.out.println(getFocusTraversalPolicy().getClass());
	}
	
	public void loadTransactions(Collection<Transaction> transactions){
		currentModel.loadTransactions(transactions);
	}
	
	public void addTransaction(Transaction transaction){
		currentModel.addTransaction(transaction);
	}
	
	public void editTransaction(Transaction transaction, String oldType){
		currentModel.editTransaction(transaction, oldType);
	}
	
	public void deleteTransaction(Transaction transaction){
		currentModel.removeTransaction(transaction);
	}

	public Transaction getSelectedTransaction(){
		Transaction transaction = null;
		Object o = getValueAt(getSelectedRow(), 0);
		if(o instanceof TransactionNode){
			transaction = ((TransactionNode)o).getTransaction();
		}
		
		return transaction;
	}
}
